package com.woyowodaows.activity.mine;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.woyowodaows.R;
import com.woyowodaows.adapter.home.HomeOrderAdapter;
import com.woyowodaows.adapter.mine.ServiceManageMentAdapter;
import com.woyowodaows.base.BaseActivity;
import com.woyowodaows.bean.home.NearRepairBean;
import com.woyowodaows.bean.home.NearRepairData;
import com.woyowodaows.fragment.mine.ServiceManagemenTremainEvaluatedFragement;
import com.woyowodaows.fragment.mine.ServiceManagementAllFragement;
import com.woyowodaows.fragment.mine.ServiceManagementHaveOrderFragement;
import com.woyowodaows.fragment.mine.ServiceManagementOffStocksFragement;
import com.woyowodaows.fragment.mine.ServiceManagementWaitingListFragement;
import com.woyowodaows.fragment.viewpager.MyFragmentPagerAdapter;
import com.woyowodaows.request.HttpAsyncTask;
import com.woyowodaows.request.HttpRequestListener;
import com.woyowodaows.request.InterfaceUrl;
import com.woyowodaows.utils.DisplayUtils;
import com.woyowodaows.utils.ToastUtil;
import com.woyowodaows.weight.LoadingDataLayout;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2019/3/28.
 * 报修管理
 */

public class ServiceManagementActivity extends BaseActivity {

//    ArrayList<String> mTitleList;
//    ArrayList<Fragment> mFragmentList;
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.rv_data)
    RecyclerView rvData;
    @BindView(R.id.sw_refresh)
    SwipeRefreshLayout swRefresh;
    @BindView(R.id.rl_load)
    LoadingDataLayout rlLoad;

//   private ServiceManageMentAdapter mAapter;
    private List<NearRepairBean> repairList=new ArrayList<>();
    private HomeOrderAdapter mAdapter;
    private String status = "0";



    @Override
    public int getLayoutId() {
        return R.layout.activity_service_mangement;
    }

    @Override
    public void initView() {
        super.initView();
        ButterKnife.bind(this);
        setTitleBar(false, "历史报修订单", "", 0, 0, null);
        mAdapter=new HomeOrderAdapter();
        LinearLayoutManager layoutManager = new LinearLayoutManager(mContext);
        rvData.setLayoutManager(layoutManager);
        rvData.setAdapter(mAdapter);
        rlLoad.setStatus(LoadingDataLayout.LOADING);
        refreshData();
    }
    private void setTabTextBlueColor(TabLayout.Tab tab) {
        TextView tv_tab = (TextView) tab.getCustomView().findViewById(R.id.tv_tab);
        tv_tab.setTextColor(Color.parseColor("#00F5FF"));
    }

    private void setTabTextGrayColor(TabLayout.Tab mTab) {
        for (int i = 0; i < tabLayout.getTabCount(); i++) {
            TabLayout.Tab tab = tabLayout.getTabAt(i);
            TextView tv_tab = (TextView) tab.getCustomView().findViewById(R.id.tv_tab);
            tv_tab.setTextColor(Color.parseColor("#666666"));
            ImageView iv_tab = (ImageView) tab.getCustomView().findViewById(R.id.iv_tab);
            iv_tab.setClickable(false);
        }
        setTabTextBlueColor(mTab);
    }
    /**
     * 设置点击事件
     *
     * @param text
     * @return
     */
    private View getTabView(String text) {
        View tabView = LayoutInflater.from(mContext).inflate(R.layout.layout_tab_title, null);
        TextView tv_tab = (TextView) tabView.findViewById(R.id.tv_tab);
        tv_tab.setText(text);
        return tabView;
    }

    @Override
    public void initData() {
        super.initData();
        DisplayUtils.setTabWidth(mContext, tabLayout, 4);
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView("全部")));
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView("待接单")));
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView("已接单")));
        tabLayout.addTab(tabLayout.newTab().setCustomView(getTabView("已完成")));
        setTabTextGrayColor(tabLayout.getTabAt(0));
    }

    private void refreshData() {
        isRefresh = true;
        pageNumber = 0;
        maxPage = 1;
        getNearRepairData();
    }
    @Override
    public void initEvent() {
        super.initEvent();
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                setTabTextGrayColor(tab);
                swRefresh.setRefreshing(true);
//                status = tab.getPosition();
                //1待接单 3.已接单 4.处理完成 不传或者传null 查全部
                switch (tab.getPosition()){
                    case 0:
                        status="0";
                     break;
                    case 1:
                        status="1";
                        break;
                    case 2:
                        status="3";
                        break;
                    case 3:
                        status="4";
                        break;

                }
                refreshData();
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });
        mAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener<NearRepairBean>() {
            @Override
            public void onItemClick(BaseQuickAdapter<NearRepairBean, ? extends BaseViewHolder> adapter, View view, int position) {
                if(repairList.size()>0){
                    NearRepairBean bean= repairList.get(position);
                    Intent intent=new Intent(mContext,OrderInformationActivity.class);
                    intent.putExtra("OrderSn",bean.getOrderSn());
                    startActivity(intent);
                }
            }
        });
        rlLoad.setOnReloadListener(new LoadingDataLayout.OnReloadListener() {
            @Override
            public void onReload(View v, int status) {
                rlLoad.setStatus(LoadingDataLayout.LOADING);
                isRefresh = true;
                pageNumber = 0;
                maxPage = 1;
                getNearRepairData();
            }
        });
        swRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                isRefresh = true;
                pageNumber = 0;
                maxPage = 1;
                getNearRepairData();
            }
        });
        mAdapter.setOnLoadMoreListener(new BaseQuickAdapter.RequestLoadMoreListener() {
            @Override
            public void onLoadMoreRequested() {
                if (maxPage <= pageNumber) {
                    mAdapter.loadMoreEnd();
                } else {
                    isRefresh = false;
                    pageNumber++;
                    getNearRepairData();
                }
            }
        }, rvData);
    }
    //获取近期订单
    public void getNearRepairData() {
        final int tempPageNum = pageNumber;
        final int tempPageSize = pageSize;
        final Map<String, Object> mapParameter = new HashMap<>();
        //  //status= 1待接单 3.已接单 4.处理完成 不传或者传null 查全部
        if (!status.equals("0")){
            mapParameter.put("status", status);
        }
        mapParameter.put("pageNumber", pageNumber);
        mapParameter.put("pageSize", pageSize);

        HttpAsyncTask.getInstance().onPostParam(mContext, "", false, InterfaceUrl.NEARREPAIR,NearRepairData.class, mapParameter, new HttpRequestListener() {
            @Override
            public void onSuccess(Object result) {
                if (result != null) {
                    NearRepairData bean = (NearRepairData) result;
                    if (bean.getResult() == HttpAsyncTask.SUCCESS) {
                        if (isRefresh) {
                            repairList.clear();
                            maxPage = bean.getData().getTotalPage();
                        }
                        if(bean.getData().getList().size()>0){
                            repairList.addAll(bean.getData().getList());
                            mAdapter.setNewData(repairList);
                        }

                    } else {
                        pageSize = tempPageSize;
                        pageNumber = tempPageNum;
                        ToastUtil.showError(mContext, bean.getMessage());
                    }
                }
                if (repairList.size() > 0){
                    rlLoad.setStatus(LoadingDataLayout.SUCCESS);
                }else {
                    rlLoad.setStatus(LoadingDataLayout.EMPTY);
                }
                swRefresh.setRefreshing(false);
                mAdapter.loadMoreComplete();

            }

            @Override
            public void onError(String message) {
                pageSize = tempPageSize;
                pageNumber = tempPageNum;
                swRefresh.setRefreshing(false);
                mAdapter.loadMoreFail();
                if (repairList.size() > 0){
                    ToastUtil.show(mContext,message);
                }else {
                    rlLoad.setStatus(LoadingDataLayout.ERROR);
                }

            }
        });

    }
}
